package com.kaibes.web.api;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import com.kaibes.web.response.ResponseFactory;

public class ResponseApi<E, S extends ResponseService<E>> {
    
    @Autowired
    protected S service;
    
    // ============POST
    protected final <T> ResponseEntity<List<T>> post(Object data, String successMessage, String failureMessage) {
        return ResponseFactory.post(service.save(data), successMessage, failureMessage);
    }
    
    // ============DELETE
    protected final ResponseEntity<Object> deleteById(Object id, String successMessage, String failureMessage) {
        return ResponseFactory.delete(service.removeById(id), successMessage, failureMessage);
    }
    protected final ResponseEntity<Object> deleteByIds(List<?> ids, String successMessage, String failureMessage) {
        return ResponseFactory.delete(service.removeByIdList(ids), successMessage, failureMessage);
    }
    
    // ============PUT
    protected final ResponseEntity<Object> putById(Object data, Object id, String successMessage, String failureMessage) {
        return ResponseFactory.put(service.updateById(data, id), successMessage, failureMessage);
    }
    protected final ResponseEntity<Object> putByIds(Object data, List<?> ids, String successMessage, String failureMessage) {
        return ResponseFactory.put(service.updateByIds(data, ids), successMessage, failureMessage);
    }
    protected final ResponseEntity<Object> putByIds(UpdateByIds<?, ?> update, String successMessage, String failureMessage) {
        return ResponseFactory.put(service.updateByIds(update.getData(), update.getIds()), successMessage, failureMessage);
    }
    
    // ============GET
    protected final ResponseEntity<E> getById(Object id, String successMessage, String failureMessage) {
        return ResponseFactory.get(service.getById(id), successMessage, failureMessage);
    }
    protected final ResponseEntity<List<E>> getByIds(List<?> ids, String successMessage, String failureMessage) {
        return ResponseFactory.get(service.listByIds(ids), successMessage, failureMessage);
    }
    
}
